<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <!-- 添加页面级别:
          type: 1, pid： "0" -->
          <el-button type="primary" size="small" @click="hAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table
          border
          row-key="id"
          :data="list"
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 业务的要求：只有页面级别的权限，才有添加（添加就是这个页面的按钮级别的权限）
              1： 页面级
              2： 按钮级
              -->

              <!-- 添加按钮级:
               type: 2, pid：当前行的id -->
              <el-button v-if="scope.row.type === 1" type="text" @click="hAdd(2, scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <el-dialog
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :visible.sync="showDialog"
      :title="isEdit? '编辑' : '添加'"
      @close="hClose"
    >
      <!-- 表单内容 -->
      <el-form ref="refForm" :model="formData" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template slot="footer">
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils/index.js'
import {
  getPermissionList,
  addPermission,
  updatePermission,
  getPermissionDetail,
  delPermission } from '@/api/permisson'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      isEdit: false, // 是否编辑
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  },
  created() {
    this.loadPermissions()
  },
  methods: {
    hSubmit() {
      this.$refs.refForm.validate(valid => {
        // valid && (this.isEdit ? this.doEdit() : this.doAdd())

        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async doAdd() {
      try {
        const res = await addPermission(this.formData)
        console.log('addPermission', res)

        this.showDialog = false
        this.loadPermissions()
      } catch (err) {
        console.log('addPermission', err)
      }
    },
    async doEdit() {
      try {
        const res = await updatePermission(this.formData)
        console.log('updatePermission', res)

        this.showDialog = false
        this.loadPermissions()
        this.$message.success('修改成功')
      } catch (err) {
        console.log('updatePermission', err)
      }
    },

    async loadPermissions() {
      try {
        const res = await getPermissionList()
        console.log('权限点', res)
        // 保存数据
        // 数组数据转成tree
        this.list = tranListToTreeData(res.data)
      } catch (err) {
        console.log('错误', err)
      }
    },
    hAdd(type, pid) {
      // 弹框
      this.showDialog = true

      this.isEdit = false // 做添加

      this.formData.pid = pid
      this.formData.type = type
    },
    async doDel(id) {
      try {
        // 1. 删除
        const res = await delPermission(id)
        console.log('删除的结果是', res)
        this.$message.success('删除成功')
        // 2. 重发请求
        this.loadPermissions()
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    hDel(id) {
      // console.log(data)
      // 弹层询问，是否删除
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
        // 用户取消删除
      })
    },

    async hEdit(id) {
      // alert(id)s
      // 1. 显示弹框
      this.showDialog = true
      // 2. 修改状态
      this.isEdit = true
      // 3. 根据id获取详情--数据回填

      try {
        const res = await getPermissionDetail(id)
        console.log('getPermissionDetail', res)
        this.formData = res.data
        // 保存数据
      } catch (err) {
        console.log('getPermissionDetail', err)
      }
    },

    hClose() {
      // 把数据清空
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      // 把校验结果清空
      this.$refs.refForm.resetFields()
    }
  }
}
</script>
